<?php
require_once 'Database.php';
//This is a utility for use in development and setup of your users.  Do not leave this utility in place on the site for production
//If you do leave it on during production, make sure the $users array is set to empty.

//TO DO:  Add authentication to this page.

//generatePassword adapted from http://www.laughing-buddha.net/php/password
function generatePassword ($length = 16)
{

	// start with a blank password
	$password = "";

	// define possible characters - any character in this string can be
	// picked for use in the password, so if you want to put vowels back in
	// or add special characters such as exclamation marks, this is where
	// you should do it
	$possible = "2346789abcdefghijkmnopqrtuvwxyzABCDEFGHIJKLMNOPQRTUVWXYZ!@#$%^&()-_+=";

	// we refer to the length of $possible a few times, so let's grab it now
	$maxlength = strlen($possible);

	// check for length overflow and truncate if necessary
	if ($length > $maxlength) {
		$length = $maxlength;
	}

	// set up a counter for how many characters are in the password so far
	$i = 0;

	// add random characters to $password until $length is reached
	while ($i < $length) {

		// pick a random character from the possible ones
		$char = substr($possible, mt_rand(0, $maxlength-1), 1);

		// have we already used this character in $password?
		if (!strstr($password, $char)) {
			// no, so it's OK to add it onto the end of whatever we've already got...
			$password .= $char;
			// ... and increase the counter by one
			$i++;
		}

	}

	// done!
	return $password;

}

try {
    $db = new Database();
    $db->connect();
    $users = array();   // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<   USE THIS LINE Except when setting up the user keys initially.
    
    //uncomment the line below and provide user names to populate the database with unique keys for each user.
    //$users = array('russ','mcgregor','mohammed', 'duhon','gabes','raza','wilde','hutchinson');
    
    //before this will work, 
    //1- the database must exist
    //2- the database user must have been created
    //3- the database user must have been GRANTed EXECUTE permissions to the database
    //4- the database tables must have been created (using airidentity.sql, which accompanies this PHP file.
    foreach ($users as $user)
	{
		echo "<h1>$user</h1>";
    	// Create the keypair
    	$res=openssl_pkey_new();
    	// Get private key
    	openssl_pkey_export($res, $privatekey);
    	// Get public key
    	$publickey=openssl_pkey_get_details($res);
    	$publickey = $publickey["key"];
        echo "Private Key:<BR/>$privatekey<br/><br/>Public Key:<BR/>$publickey<BR/><BR/>";
        $cleartext = '1234 5678 9012 3456';
        echo "Clear text:<br/>$cleartext<BR/><BR/>";
        openssl_public_encrypt($cleartext, $crypttext, $publickey);
        echo "Crypt text:<br/>$crypttext<BR/><BR/>";
        openssl_private_decrypt($crypttext, $decrypted, $privatekey);
        echo "Decrypted text:<BR/>$decrypted<br/><br/>";
        $secretkey = generatePassword(16);
        echo "Symmetric Key:<BR/>$secretkey<br/><br/>";
    	sleep(1);  //sleep is here because uniqid needs time as a seed.  We want different unique ids, so we want different times to seed them
        //$db->insertUser($user, $privatekey, $publickey, $secretkey);
        //UNCOMMENT the line above and comment the line below if you change the system to implement public key encryption.
        $db->insertUser($user, 'private', 'public', $secretkey);
        
    }
} catch (Exception $ee) {
    echo $ee->getMessage();
    var_dump($ee);
}